package cn.mxz.generator;

import java.io.File;
import java.util.List;

import cn.mxz.generator.base.GeneratorImpl;
import cn.mxz.generator.config.GeneratorConfig;
import cn.mxz.generator.config.IClass;
import cn.mxz.generator.config.IMethod;
import cn.mxz.generator.config.IReturn;
import cn.mxz.generator.util.StringUtils;

public class ASDataHandlerGenerator extends GeneratorImpl {

	private static final String TAIL = "Handler";

	public ASDataHandlerGenerator(GeneratorConfig config) {
		super(config, "client_src.handler");
	}
	
	@Override
	public void generator() {
		List<IClass> all = config.getClazzs();
		
		String content = config.getTemplet("ASDataHandler");
		
		StringBuilder cases = new StringBuilder();
		for (IClass c : all) {
			for (IMethod m : c.getMethods()) {
				cases.append(buildCase(c, m, config));
			}
		}
		content = content.replaceAll("HANDLER_CASES", cases.toString());
		content = content.replaceAll("PACKAGE_NAME", getPackageName());
		content = content.replaceAll("IMPORTS", buildImports(all));
	
		String pack = config.getNodeText("client_src.handler_manager.package");
		content = content.replaceAll("HANDLERS_PACKAGE", pack);
		
		config.getFileUtil(getSrcPath() + File.separator + "DataHandler.as").writeToFile(content);
	}

	private String buildCase(IClass c, IMethod m, GeneratorConfig config) {
		String content = config.getTemplet("ASHandlerCase");

		
		content = content.replace("PACKET_NUMBER", m.getId() + "");
		content = content.replace("HANDLER_NAME", c.getInterface().getName() + TAIL);

		String mn = ASHandlerInterfaceGenerator.buildMethodName(m.getName());
		content = content.replace("METHOD_NAME", mn);

		content = content.replaceAll("MERGE_FROM", buildMergeFrom(m));
		content = content.replaceAll("INSERT_FIELDS", buildFileds(m));
		
		content = content.replaceAll("ACTION_NAME", c.getBeanName());

		content = content.replaceAll("EVENT_NAME", getEventName(m));
		content = content.replaceAll("EVENT_CLASS_NAME", ASEventsGenerator.buildClassName(c, m));
		
		if(!m.getReturn().isVoid()) {
			content = content.replaceAll("EVENT_FILED", getVarName(m));
		} else {
			content = content.replaceAll("EVENT_FILED", "");
		}
		
		return content;
	}

	private String buildFileds(IMethod m) {
		IReturn rt = m.getReturn();
		return rt.isVoid() ? "" : getVarName(m);
	}


	private String getEventName(IMethod m) {
		return "e" + m.getId();
	}
	
	private String getVarName(IMethod m) {
		return "m" + m.getId();
	}

	private String buildMergeFrom(IMethod m) {
		IReturn rt = m.getReturn();
		if(rt.isVoid()) {
			return "";
		} else {
			String typeSimple = rt.getTypeSimple();
			
			String varName = getVarName(m);
			
			if(rt.isBaseType()) {
				String type = StringUtils.firstToUpperCase(rt.getType());
				
				if(type.equals("String")) {
					type = "UTF";
				}
				
				return 
						"					var " + varName + ":" + typeSimple + " = data.read" + type + "();";
			} else {
				return
						"					var " + varName + ":" + typeSimple + " = new " + typeSimple +  "();\r" +
						"					" + varName + ".mergeFrom(data);";
			}
		}
	}
}
